using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// 培训_培训计划
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class PX_PlanController : CoreApiController
    {
        private readonly PX_PlanService _service;
        /// <summary>
        /// PX_Plan
        ///</summary>
        public PX_PlanController(PX_PlanService service)
        {
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<PX_PlanDto> pagingQueryModel)
        {
            IPageModel<PX_PlanDto>
                result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.Nameoftrainingplan,//培训计划名称
                a.TrainingBeginTime,//培训开始时间
                a.Trainingvenues,//培训地点
                a.Trainingmethods,//培训方式
                a.TrainingmethodsText,//培训方式
                a.Trainingcontent,//培训内容
                a.State,//状态(是否结束)
                a.StateText,//状态(是否结束)
                a.Target,//目标
                Examinationscore = a.PX_ExamUser?.FirstOrDefault()?.Examinationscore ?? 0,//考试得分
                IsQualified = a.PX_ExamUser?.FirstOrDefault()?.IsQualified ?? "0",//
                //a.TrainingMatrix,//培训矩阵ID
                //a.HeadofTraining,//培训负责人
                //a.Trainingbudget,//培训预算
                //a.TrainingEndTime,//培训结束时间
                //a.Trainer,//培训讲师
                //a.trainingcourse,//培训课程ID
                //a.CreateFactory,//隶属工厂
                //a.CreateDept,//
                //a.UpdateDept,//
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult> SaveAsync(PX_PlanDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }


        /// <summary>
        /// 培训心得添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("XinDeSave")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.XinDeSaveAsync), "培训心得")]
        public async Task<IActionResult> XinDeSaveAsync(PX_PlanUserDto dto)
        {
            dto.PersonID = UserIdentity.Id;
            await _service.XinDeSaveAsync(dto);
            return this.JsonSuccess();
        }


        /// <summary>
        /// XinDeGet
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("XinDeGet")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.XinDeGetAsync), "获取培训心得")]
        public async Task<IActionResult> XinDeGetAsync(Guid id)
        {
            PX_PlanUserDto dto = new PX_PlanUserDto()
            {
                PlanID = id,
                PersonID = UserIdentity.Id
            };

            var model = await _service.GetPX_PlanUserAsync(dto);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(new
            {
                model.PlanID,
                model.Experience,
                model.Advice,
                model.Content
            });
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(PX_PlanController) + "_" + nameof(PX_PlanController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                model.TrainingMatrix,//培训矩阵ID
                model.Nameoftrainingplan,//培训计划名称
                model.Trainingcontent,//培训内容
                //model.HeadofTraining,//培训负责人
                //model.Trainingbudget,//培训预算
                //model.TrainingBeginTime,//培训开始时间
                //model.TrainingEndTime,//培训结束时间
                //model.Trainingvenues,//培训地点
                //model.Trainer,//培训讲师
                //model.Trainingmethods,//培训方式
                //model.trainingcourse,//培训课程ID
                //model.State,//状态(是否结束)
                //model.CreateFactory,//隶属工厂
                //model.Target,//目标
                //model.CreateDept,//
                //model.UpdateDept,//
            };
            return this.JsonSuccess(retObj);
        }


        [HttpGet("PCGet")]
        public async Task<IActionResult> PCGet(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            
            return this.JsonSuccess(model);
        }
        /// <summary>
        /// 获取当前用户指定培训计划的学习时间
        /// </summary>
        /// <param name="PlanID"></param>
        /// <returns></returns>
        [HttpGet("GetCurrUserCourseHours")]
        public async Task<IActionResult> GetCurrUserCourseHours(Guid PlanID)
        {
            PX_PlanUserDto dto = new PX_PlanUserDto()
            {
                PlanID = PlanID,
                PersonID = UserIdentity.Id
            };

            var model = await _service.GetPX_PlanUserAsync(dto);
            return this.JsonSuccess(new
            {
                model?.CourseHours,
            });
        }


        /// <summary>
        /// 累加当前用户指定培训计划的学习时间
        /// </summary>
        /// <param name="PlanID"></param>
        /// <returns></returns>
        [HttpGet("TotalizerMinute")]
        public async Task<IActionResult> TotalizerMinute(Guid PlanID)
        {
            PX_PlanUserDto dto = new PX_PlanUserDto()
            {
                PlanID = PlanID,
                PersonID = UserIdentity.Id
            };
            var model = await _service.TotalizerMinute(dto);
            return this.JsonSuccess(new
            {
                model?.CourseHours,
            });
        }
        #endregion


    }
}
